package com.chunuo.blogs.service.impl;

import com.chunuo.blogs.dao.ClassifyDao;
import com.chunuo.blogs.entity.Classify;
import com.chunuo.blogs.service.ClassifyService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.*;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 分类
 *
 * @author liujiamiao
 * @email 2544296058@qq.com
 * @date 2020-05-17 12:11
 */
@CacheConfig(cacheManager = "redisCacheManager",cacheNames={"classify"})
@Service
public class ClassifyServiceImpl implements ClassifyService {

    @Autowired
    private ClassifyDao classifyDao;

    @Cacheable(key = "#page+'-'+#size",value = "classifyAll")
    @Override
    public List<Classify> findAll(Integer page,Integer size) {
        if(page!=0)
            PageHelper.startPage(page,size);
        return classifyDao.findAll();
    }


    @Cacheable(key = "#id")
    @Override
    public Classify findById(Integer id) {
        return classifyDao.findById(id);
    }

    @CacheEvict(value = "classifyAll",allEntries = true)
    @CachePut(key = "#result.id")
    @Override
    public Classify add(Classify classify) {
        classifyDao.add(classify);
        return classify;
    }

    @Caching(evict = {@CacheEvict(key = "#id"),@CacheEvict(value = "classifyAll",allEntries = true)})
//    @CacheEvict(key = "#id")
    @Override
    public void delete(Integer id) {
        classifyDao.delete(id);
    }

    @CacheEvict(value = "classifyAll",allEntries = true)
    @CachePut(key = "#result.id")
    @Override
    public Classify update(Classify classify) {
        classifyDao.update(classify);
        return classify;
    }
}
